package com.liu.udpDemo;

import java.io.FileInputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class UdpClient {
    public static void main(String[] args) {
//        udp协议不需要双方连接，只需要发送包就可以了
        DatagramSocket datagramSocket = null;
        try {
//          1.建立一个socket
            datagramSocket = new DatagramSocket();
//            建一个包
            String str="hello!";
            InetAddress address=InetAddress.getByName("127.0.0.1");
            FileInputStream fis=new FileInputStream("D:\\test123.jpg");
            byte[] bytes = new byte[1024];
            int len;
            while ((len=fis.read(bytes))!=-1){

            }
//                                                            要发送的数据       数据开始   数据结束       要发送的IP地址  +  端口号
            DatagramPacket datagramPacket =new DatagramPacket(bytes,0,bytes.length,address,9876);
//            发送一包
            datagramSocket.send(datagramPacket);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            datagramSocket.close();
        }
    }
}
